<template>
    <el-container class="home_container">
      <!-- 头部 -->
      <el-header class="img_text_box ">
        <div>
          <img src="../assets/logo.png" alt="" />
          <span>电商后台管理</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <div class="toggle-button" @click="toggleCollapse">
            <!-- {{ isCollapse ? '展开▷' : '◁收起' }} -->
            |||
          </div>
          <!-- 菜单栏 -->
          <el-menu background-color="#333744" text-color="#fff" active-text-color="#409EFF" :unique-opened="true"
            :collapse-transition="false" :collapse="isCollapse" :router="true" :default-active="activePath">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconsObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id"
                @click="saveNacState('/' + subItem.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subItem.authName }}</span>
                </template>
                </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 内容主体 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </template>
        
  <script>
  export default {
    name: 'Home',
    data() {
      return {
        menuList: [],
        iconsObj: {
          125: 'el-icon-user-solid',
          103: 'el-icon-s-help',
          101: 'el-icon-s-shop',
          102: 'el-icon-s-claim',
          145: 'el-icon-s-marketing',
        },
        isCollapse: false,
        activePath: '',
      }
    },
    created() {
      this.getMenuList();
      this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
      //折叠菜单
      toggleCollapse() {
        this.isCollapse = !this.isCollapse
      },
      //退出登陆
  
      async logout() {
        await this.$confirm('正在退出管理系统, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            window.sessionStorage.clear()
            this.$router.push('/login')
            this.$message.success('退出成功！')
          })
          .catch(() => {
            this.$message.info('已取消删除')
          })
      },
      //获取菜单列表
      async getMenuList() {
        const { data: res } = await this.$http.get('menus')
        if (res.meta.status !== 200) return this.$message.error(res.meta.mag)
        this.menuList = res.data
      },
      //保存链接激活状态
      saveNacState(activePath) {
        window.sessionStorage.setItem('activePath', activePath)
        this.activePath = activePath
        console.log(this.activePath);
      },
    },
  }
  </script>
  
  <style scoped>
  .right {
    position: absolute;
    top: 10px;
    right: 50px;
    background-color: rgb(69, 69, 69);
  }
  
  .el-container {
    height: 100%;
  }
  
  .el-header {
    background: #333744;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: white;
    font-size: 22px;
    font-weight: 600;
    letter-spacing: 0.2em;
    text-shadow: 2px 2px 10px #ccc;
  }
  
  
  .el-aside {
    transition: all 0.8s;
    background: #333744;
  }
  
  /* 导致右侧多1px 优化 */
  .el-aside .el-menu {
    border-right: 0px;
  }
  
  .el-main {
    background-color: #fff;
  }
  
  .img_text_box {
    height: 100%;
  }
  
  .img_text_box img {
    width: 60px;
    height: 90%;
    border-radius: 50%;
    margin: 5px;
    /* fill-opacity: inherit; */
  }
  
  .img_text_box span {
    position: absolute;
    top: 5px;
    margin-left: 15px;
  }
  
  b {
    position: absolute;
    top: 35px;
    font-size: 12px;
    font-weight: 400;
    letter-spacing: 0.5em;
    margin-left: 15px;
  }
  
  .toggle-button {
    background-color: #545b66;
    font-size: 10px;
    line-height: 30px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.3em;
    cursor: pointer;
  }
  
  .toggle-button:hover {
    background-color: #474d56
  }
  </style>
  